/**
 * Created by Administrator on 2017/10/18.
 */
var AppVersion = {
    options : {
        url : {
            "list": rootPath + "/s/appversion/list",
            "remove" : rootPath + "/s/appversion/remove" ,
            "detail": rootPath + "/s/appversion/get",
            "save" : rootPath + "/s/appversion/save"
        }
    }
};

AppVersion.queryList = function(){
    var _this = this;
    $("#datagrid").datagrid(
    {

        url : _this.options.url.list,
            pagination:true,//分页控件
        checkOnSelect: true,
        rownumbers: true,
        selectOnCheck: true,
        nowrap : false,
        singleSelect:false,
        autoRowHeight:true,
        fitColumns: true,
        pageNumber :_this.page,
        pageSize : _this.pageSize,
        queryParams : _this.getQueryParam(),
        loadFilter : function (data) {
        if(data.opStatus == "0"){
            return data.data;
        }

    },
        columns : [ [  {
            field:'_',
            checkbox:true,
            width:'5%'
        },{
            field : 'subscribe',
            title : '主题',
            width : '15%',
            align : 'center'
        }, {
            field : 'versionNo',
            title : '版本号',
            width : '15%',
            align : 'center'
        }, {
            field : 'content',
            title : '内容',
            width : '30%',
            align : 'center'
        } ,{
            field : 'appType',
            title : '类型',
            align : 'center',
            width : '10%',
            formatter:function(data){
                if(data == "1"){
                    return "车主APP";
                }else{
                    return "发卡APP";
                }

            }
        }, {
            field : 'publishTime',
            title : '发布时间',
            align : 'center',
            width : '10%'

        }, {
            field : 'isForce',
            title : '强制更新',
            width : '10%',
            align : 'center',
            formatter:function(data){
                if(data == "1"){
                    return "是";
                }else{
                    return "否";
                }

            }

        },{
            field : 'id',
            title : '操作',
            width :'5%',
            align : 'center',
            formatter:function(data){
                var str = '<button class="btn btn-default btn-xs" type="button" onclick="AppVersion.toEditPage(\''+data+'\')"><i class="glyphicon glyphicon-pencil"></i> 修改</button>';
                return str;
            }
        }] ],
            onDblClickRow : function(index,row){
    }

    });
};

AppVersion.getQueryParam = function(){

    var data = {
        versionNo : $("#search-form input[name='versionNo']").val(),
        subscribe : $("#search-form input[name='subscribe']").val(),
        appType : $("#appType").val()
    };
    return data;

};

AppVersion.toAddPage = function(){
    AppVersion.resetForm();
    $("#editModal").modal('show');
};

AppVersion.toEditPage = function(id){
    $("#appVersionId").val(id);
    $.ajax({
        type : "get",
        url : AppVersion.options.url.detail + "/" + id,
        success :function (data) {
            if(data.opStatus == "0"){

                $("#appVersionForm").form("load",data.data);
                $("#editModal").modal('show');
            }
        },
        error : function (e) {

        }
    })

};

AppVersion.remove = function(ids){
    $.ajax({
        type : "get",
        url : AppVersion.options.url.remove ,
        data : {ids : ids},
        success :function (data) {
            if(data.opStatus == "0"){
                AppVersion.queryList();
                showMsg("删除成功");

            }else{
                showMsg("删除失败！");
            }
        },
        error : function (e) {
            showMsg("服务器未知错误");

        }
    })
};
AppVersion.save =function(){
    var data = $("#appVersionForm").serialize();
    $.ajax({
        type : "post",
        url : AppVersion.options.url.save ,
        data : data,
        success :function (res) {
            if(res.opStatus == "0"){
                AppVersion.queryList();
                AppVersion.resetForm();
                showMsg("保存成功");
                $("#editModal").modal("hide");

            }else{
                showMsg("保存失败！");
            }
        },
        error : function (e) {
            showMsg("服务器未知错误");

        }
    })
};
AppVersion.resetForm = function () {
    $("#appVersionForm input[name='id']").val("");
    $("#appVersionForm input[name='versionNo']").val("");
    $("#appVersionForm input[name='subscribe']").val("");
    $("#appVersionForm input[name='url']").val("");
    $("#appVersionForm input[name='publishTime']").val("");
    // $("#appVersionForm input[name='appType']").val("");
    // $("#appVersionForm input[name='isForce']").val("");
    $("#appVersionForm textarea[name='content']").val("");
}

$(document).ready(function () {
    AppVersion.queryList();
    $("#search_button").click(function () {
        AppVersion.queryList();
    });
    $("#add_button").click(function () {
        AppVersion.toAddPage();
    });
    $("#remove_button").click(function(){
        var rows = $("#datagrid").datagrid("getChecked");
        if(rows.length > 0){
            showConfirm("是否确认删除？",function () {
                var ids = [];
                for(var i in rows ){
                    ids.push(rows[i].id);
                }
                AppVersion.remove(ids.join(","));
            });
        }else {
            showMsg("请选择要删除的用户！");
        }
    })
});
